//
//  Exi+UIButton.swift
//  SinaCaoliang
//
//  Created by 曹亮 on 16/10/22.
//  Copyright © 2016年 曹亮. All rights reserved.
//

import UIKit

extension UIButton{

    
    convenience init(setImgName:String? = nil, backgroundImgName:String? = nil, title:String? = nil, titleColor:UIColor? = nil, target :Any? = nil ,action:Selector? = nil) {

        self.init()

        //设置按钮标题
        if title != nil {

            self.setTitle(title, for: UIControlState.normal)
            self.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            if titleColor != nil {
                self.setTitleColor(titleColor, for: UIControlState.normal)
                self.setTitleColor(CLTHEMECOLOR, for: UIControlState.highlighted)
            }
        }

        //设置按钮图片
        if let imgN = setImgName {
            self.setImage(UIImage.init(named: imgN)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), for: UIControlState.normal)
            self.setImage(UIImage.init(named: "\(imgN)_highlighted")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), for: UIControlState.highlighted)
        }

        //设置按钮背景图片
        if let imgB = backgroundImgName {
            self.setBackgroundImage(UIImage.init(named: imgB)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), for: UIControlState.normal)
            self.setBackgroundImage(UIImage.init(named: "\(imgB)_highlighted")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), for: UIControlState.highlighted)

        }

        //按钮添加方法
        if target != nil && action != nil {
            //添加方法
            self.addTarget(target, action: action!, for: UIControlEvents.touchUpInside)

        self.sizeToFit()

        }
    }
}
